package com.leyou.item.service.impl;

import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.ISpecParamService;
import com.leyou.test.enums.ExceptionEnum;
import com.leyou.test.exceptions.LyException;
import com.leyou.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpecParamService implements ISpecParamService {
    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 根据规格分组的gid、分类的cid、是否需要被搜索searching，查询规格参数param
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDTO> querySpecParams(Long gid,Long cid,Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        //根据SpecParam的非空属性进行查询规格参数
        List<SpecParam> list = specParamMapper.select(specParam);
        //对查询结果进行判断
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }
}
